package com.itheima.reggie.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Dish;
import com.itheima.reggie.entity.DishFlavor;
import com.itheima.reggie.mapper.CategoryMapper;
import com.itheima.reggie.mapper.DishFlavorMapper;
import com.itheima.reggie.mapper.DishMapper;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.DishFlavorService;
import com.itheima.reggie.service.DishService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


@Service



public class DishServiceImpl extends ServiceImpl<DishMapper, Dish> implements DishService {

    @Autowired
    DishFlavorService dishFlavorService;

    @Autowired
    CategoryService categoryService;

    // TODO: 2022/2/26 0026
    public void saveAndFlavor(DishDto dishDto){
/*
        this.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();*/



        //保存菜品的基本信息到菜品表dish
        this.save(dishDto);

        List<DishFlavor> flavors = dishDto.getFlavors();
        //设置菜品与口味的关系
        flavors.forEach(flavor -> flavor.setDishId(dishDto.getId()));

        //保存菜品口味数据到菜品口味表dish_flavor
        dishFlavorService.saveBatch(flavors);
    }

    @Override
    @Transactional
    public DishDto getByIdAndFlavor(Long id) {

        Dish dish = this.getById(id);
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, id);
        List<DishFlavor> list = dishFlavorService.list(queryWrapper);
        DishDto dishDto = new DishDto();
        BeanUtils.copyProperties(dish, dishDto);
        dishDto.setFlavors(list);

        return dishDto;
    }

    @Override
    @Transactional
    public void updateAndFlavor(DishDto dishDto) {
        //更新菜品表
        this.updateById(dishDto);
        LambdaQueryWrapper<DishFlavor> queryWrapper = Wrappers.lambdaQuery(DishFlavor.class).eq(DishFlavor::getDishId, dishDto.getId());

        //删除菜品id对应口味表中的数据
        dishFlavorService.remove(queryWrapper);
        List<DishFlavor> flavors = dishDto.getFlavors();
        flavors.forEach(dishFlavor ->
                dishFlavor.setDishId(dishDto.getId())
        );

        dishFlavorService.saveBatch(flavors);

    }

    //分页查询菜单表,并返回前端包装类(含有分类名称)
    @Override
    public Page<DishDto> page(Integer page, Integer pageSize, String name) {

        Page<Dish> page1 = new Page<>(page, pageSize);
        LambdaQueryWrapper<Dish> queryWrapper = Wrappers.lambdaQuery(Dish.class).like(StringUtils.isNotBlank(name), Dish::getName, name);
        this.page(page1, queryWrapper);

        Page<DishDto> page2 = new Page<>(page, pageSize);

        BeanUtils.copyProperties(page1, page2, "records");
        ArrayList<DishDto> records = new ArrayList<>();
        List<Dish> records1 = page1.getRecords();

        for (Dish dish : records1) {
            DishDto dishDto = new DishDto();
            Category category = categoryService.getById(dish.getCategoryId());
            dishDto.setCategoryName(category.getName());
            BeanUtils.copyProperties(dish, dishDto);
            records.add(dishDto);
        }

        page2.setRecords(records);
        return page2;
    }

}
